﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;

namespace NJL.WebManage.LocalColor
{
    public partial class LocalColorManage : BasePage
    {
        PagedDataSource pds;
        private readonly BLL.NJL_LOCAL_COLOR bll = new BLL.NJL_LOCAL_COLOR();
        private readonly BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        protected IList<Model.NJL_CS_AREA> ListArea;
        protected string AreaCode = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["Page"]))
                {
                    ViewState["pager"] = Request["Page"];
                }
                else
                {
                    ViewState["pager"] = "0";
                }
                bind();
            }
        }

        private void LoadData()
        {
            ListArea = bllArea.GetModelList(" deep=1");
        }

        public void bind()
        {
            LoadData();

            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();


            if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
            {
                AreaCode = Request.QueryString["areacode"];
            }

            pds.DataSource = bll.GetList(GetSelectWhere()).Tables[0].DefaultView;

            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }

            lit.Text = bll.GetListCount(GetSelectWhere()).ToString();

            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptUserList.DataSource = pds;
            rptUserList.DataBind();
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_LOCAL_COLOR model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }

                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "imgbtn1")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_LOCAL_COLOR model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.IS_TOP == 1)
                    {
                        model.IS_TOP = 0;
                    }
                    else
                    {
                        model.IS_TOP = 1;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "置顶失败");
                }
            }
            else if (e.CommandName == "imgbtn2")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_LOCAL_COLOR model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.IS_INDEX == 1)
                    {
                        model.IS_INDEX = 0;
                    }
                    else
                    {
                        model.IS_INDEX = 1;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "设为首页显示失败");
                }
            }
            else if (e.CommandName == "imgbtn3")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_LOCAL_COLOR model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.IS_PUBLISH == 1)
                    {
                        model.IS_PUBLISH = 0;
                    }
                    else
                    {
                        model.IS_PUBLISH = 1;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "设为发布状态失败");
                }
            }
            else if (e.CommandName == "imgbtn4")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_LOCAL_COLOR model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.SHOW_STATE == 1)
                    {
                        model.SHOW_STATE = 0;
                    }
                    else
                    {
                        model.SHOW_STATE = 1;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "设为显示状态失败");
                }
            }
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();
                ImageButton imgbtn = e.Item.FindControl("imgbtn1") as ImageButton;
                if (imgbtn.AlternateText == "1")
                {
                    imgbtn.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn.AlternateText == "0")
                {
                    imgbtn.ImageUrl = "~/Images/false.gif";
                }
                ImageButton imgbtn2 = e.Item.FindControl("imgbtn2") as ImageButton;
                if (imgbtn2.AlternateText == "1")
                {
                    imgbtn2.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn2.AlternateText == "0")
                {
                    imgbtn2.ImageUrl = "~/Images/false.gif";
                }
                ImageButton imgbtn3 = e.Item.FindControl("imgbtn3") as ImageButton;
                if (imgbtn3.AlternateText == "1")
                {
                    imgbtn3.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn3.AlternateText == "0")
                {
                    imgbtn3.ImageUrl = "~/Images/false.gif";
                }
                ImageButton imgbtn4 = e.Item.FindControl("imgbtn4") as ImageButton;
                if (imgbtn4.AlternateText == "1")
                {
                    imgbtn4.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn4.AlternateText == "0")
                {
                    imgbtn4.ImageUrl = "~/Images/false.gif";
                }
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                string imgurl = GetImageUrl(id);
                Image img = e.Item.FindControl("Image1") as Image;
                ViewState["iamgeid"] = "";
                if (imgurl != "")
                {
                    img.ImageUrl = imgurl;
                    ViewState["iamgeid"] = imgurl;
                }
                else
                {
                    img.Visible = false;
                }
            }
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {

            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                NJL.Model.NJL_LOCAL_COLOR model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }

        /// <summary>
        /// 根据地区编号或网站编号获取信息统计数据
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        protected int GetListCountByAreaCode(string areaCode, string showState)
        {
            //只有总站管理员才能看到统计数据
            if (USER_TYPE_CODE == "1")
            {
                if (showState != "")
                {
                    return bll.GetListCount(GetSelectWhereCount() + " and NJL_LOCAL_COLOR.NJL_SYS_BASE_ID = '" + areaCode + "' and NJL_LOCAL_COLOR.SHOW_STATE=" + showState);
                }
                else
                {
                    return bll.GetListCount(GetSelectWhereCount() + " and NJL_LOCAL_COLOR.NJL_SYS_BASE_ID = '" + areaCode + "'");
                }
            }
            else
            {
                //
                return bll.GetListCount(GetSelectWhereCount() + " and NJL_LOCAL_COLOR.NJL_SYS_BASE_ID = '" + areaCode + "'");
            }
        }

        /// <summary>
        /// 获取当前页面数据查询条件
        /// </summary>
        /// <returns></returns>
        private string GetSelectWhere()
        {
            string strWhere = " 1=1 ";
            if (WEBSITE_CODE == "1")
            {
                if (AreaCode != "")
                {
                    strWhere = strWhere + " and NJL_LOCAL_COLOR.NJL_SYS_BASE_ID='" + AreaCode + "'";
                }
            }
            else
            {
                strWhere = strWhere + " and NJL_LOCAL_COLOR.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'";

            }
            strWhere += " and NJL_LOCAL_COLOR.NEWS_TITLE like '%" + txtKeywords.Text.Trim() + "%'";
            return strWhere;
        }

        /// <summary>
        /// 获取当前页面统计数据的条件(把区域判断去掉了)
        /// </summary>
        /// <returns></returns>
        private string GetSelectWhereCount()
        {
            string strWhere = " 1=1 ";

            if (WEBSITE_CODE != "1")
            {
                strWhere = strWhere + " and NJL_LOCAL_COLOR.CREATE_USER_ID='" + USER_ID + "'";
            }

            if (USER_TYPE_CODE == "2")
            {
                strWhere = strWhere + " and NJL_LOCAL_COLOR.SHOW_STATE=1 ";
            }

            return strWhere;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }

    }
}